package com.jiuyun.dao.impi;

import com.jiuyun.dao.ExamDao;
import com.jiuyun.entity.Exam;
import com.jiuyun.entity.Exam1;
import com.jiuyun.util.AliPool;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class ExamDaoImpl implements ExamDao {

    PreparedStatement pst;
    Connection conn;
    QueryRunner qr=new QueryRunner();
    QueryRunner runner=new QueryRunner();
    @Override
    public List<Exam> examfind() throws SQLException {
        String sql="select * FROM exam";
        conn= AliPool.getConn();
        List list=qr.query(conn,sql,new BeanListHandler<Exam>(Exam.class));
        conn.close();
        return list;
    }

    @Override
    public List<Exam> examidfind(String eid) throws SQLException {
        String sql="select * from exam where exam_id=?";
        conn=AliPool.getConn();
        List list=qr.query(conn,sql,new BeanListHandler<Exam>(Exam.class),eid);
        conn.close();
        return list;
    }

    @Override
    public int examdel(String eid) throws SQLException {
        String sql="delete from exam where exam_id=?";
        conn=AliPool.getConn();
        int del=qr.update(conn,sql,eid);
        conn.close();
        return del;
    }

    @Override
    public List<Exam1> examfind(String exam_id) throws SQLException {
        String sql="select * from exam where exam_id=?";
        conn= AliPool.getConn();
        List<Exam1> list=runner.query(conn,sql,new BeanListHandler<Exam1>(Exam1.class),exam_id);
        conn.close();
        return list;

    }

    @Override
    public Exam1 find(String exam_id) throws SQLException {
        String sql="select * from exam where exam_id=?";
        conn= AliPool.getConn();
        Exam1 exam=runner.query(conn,sql,new BeanHandler<Exam1>(Exam1.class),exam_id);
        conn.close();
        return exam;
    }

    @Override
    public Exam1 find1(String exam_name) throws SQLException {
        String sql="select * from exam where exam_name=?";
        conn= AliPool.getConn();
        Exam1 exam=runner.query(conn,sql,new BeanHandler<Exam1>(Exam1.class),exam_name);
        conn.close();
        return exam;
    }

    public static void main(String[] args) throws SQLException {
        ExamDao dao=new ExamDaoImpl();
        System.out.println(dao.examidfind("1"));
        dao.examdel("nm123");
    }
}
